<template>
	<view class="pages">
		
		
			<yy-uploadpictures v-model="formData['moment']" 
				:config='config'>
				<view>
					<u--textarea v-model="formData.name" placeholder="请输入活动相关内容" border="none" placeholderStyle="{color:'#B3B1B2';font-size:'28rpx'}" height="140"></u--textarea>
					
				</view>
			</yy-uploadpictures>
		
		<view class="submit" @click="submit" v-if="id==0">
			<text>发起活动</text>
		</view>
		<view class="submit" v-else @click="edit_Activity">
			<text>完成</text>
		</view>
	</view>
</template>

<script>
	import COS from 'cos-js-sdk-v5';
	import cos from '@/common/cos.js'
	const app = getApp();
	import {
		addActivity,
		getActivityOne,
		editActivity
	} from '@/common/api.js'
	export default {
		data() {
			return {
				autoHeight:true,
				$http_cos:app.$http_cos,
				config: {
					id: 3,
					field: 'chunk',
					maxCount: 6,
					array: [],
					width: '202',
					height: '202',
					tip: '',
					slot:true,
				},
				
				formData: {
					moment:"[]",
					name: '',
					id:''
				},
				id:0
			}
		},
		onLoad(option) {
			if(option.id){
				this.id=option.id
				this.get_ActivityList(option.id)
			}
		},
		watch: {
			formData: {
				handler(value) {
					console.log('深度监听', value)
				},
				deep: true // 深度监听
			},
		},
		methods: {
			edit_Activity(){
				uni.showLoading({
					title: '修改中'
				});
				this.formData.id=this.id
				editActivity(this.formData, {
					custom: {
						auth: false,
						toast: false,
						catch: false
					}
				}).then((result) => {
						uni.$u.toast('修改成功')
						// uni.switchTab({
						// 	url:'/pages/moment/index'
						// })
					this.return_beforePage()
					
				}).catch((err) => {
					uni.hideLoading();
				})
			},
			get_ActivityList(id) {
				let data = {
					params: {
						id: id
					}
			
				}
				getActivityOne(data, {
					custom: {
						auth: false,
						toast: false,
						catch: false
					}
				}).then((result) => {
					
					this.formData = result
				}).catch((err) => {
					
				})
			},
			pickExclude(obj, keys) {
				// 某些情况下，type可能会为
			    if (!['[object Object]', '[object File]'].includes(Object.prototype.toString.call(obj))) {
			        return {}
			    }
			    return Object.keys(obj).reduce((prev, key) => {
			        if (!keys.includes(key)) {
			            prev[key] = obj[key]
			        }
			        return prev
			    }, {})
			},
			formatImage(res) {
			    return res.tempFiles.map((item) => ({
			        ...this.pickExclude(item, ['path']),
			        type: 'image',
			        url: item.path,
			        thumb: item.path,
					size: item.size,
					// #ifdef H5
					name: item.name
					// #endif
			    }))
			},
			chooseImage(){
				return new Promise((resolve, reject) => {
				        uni.chooseImage({
				            count: 1,
				            success: (res) => resolve(this.formatImage(res)),
				            fail: reject
				        })
				})
			},
			async change_cover(){
				let list=[]
					let tempFiles=await  this.chooseImage()
					for(let i=0;i<tempFiles.length;i++){
						let name = tempFiles[i].name
						let index = tempFiles[i].name.indexOf('.')
						let name1 = name.substring(0, index)
						let name2 = name.substring(index, name.length)
						tempFiles[i].name = name1 + cos.uuid() + name2
						const blob = await cos.objectURLToBlob(tempFiles[i].url, tempFiles[i].name)
						 const result = await cos.uploadFilePromise(blob, tempFiles[i].name)
						list.push('https://'+result+'?imageView2/2/w/750/h/q/85')
					}
					this.formData.cover=list.toString()
			},
			return_beforePage(){
				let pages = getCurrentPages()
				console.log('aaaa',pages)
				if(pages.length>0){
					
					let beforePage = pages[pages.length - 2]
					console.log(beforePage)
					// #ifdef H5
					console.log('aaa')
					
					uni.switchTab({
						url:'/pages/moment/index',
						success: () => {
							console.log()
							beforePage.$vm.init_info()
						}
					})
					// #endif
					// #ifndef H5
					beforePage.init_info()
					// #endif
					console.log('aaabcb')
					
				}else{
					uni.switchTab({
						url:'/pages/moment/index'
					})
				}
			},
			submit() {
				uni.showLoading({
					title: '发布中'
				});
				addActivity(this.formData, {
					custom: {
						auth: true,
						toast: false,
						catch: true
					}
				}).then((r) => {
					uni.$u.toast('发布成功')
					this.return_beforePage()
				}).catch((err) => {
					console.log(err)
					uni.hideLoading();
				})
			}

		}
	}
</script>

<style lang="scss" scoped>
	.pages{
		background-color: #fff;
		height:100vh;
		
		/deep/ .main{
			padding:55rpx 48rpx;
			.long_title{
				font-size: 28rpx;
				font-family: Microsoft YaHei;
				font-weight: 400;
				color: #B3B1B2;
			}
		}
		.btn_public {
			position: fixed;
			bottom: 134rpx;
			left: 55rpx;
			right: 55rpx;
			line-height: 92rpx;
			overflow: hidden;
		}
	}
	
</style>
